define(['./../initial', './../service/symbolFactory', './../service/renderPointsOnMap', './../service/zoomMap', './autoSurveyControl'], function() {
    var app = angular.module('App.home');
    app.factory('ProjectMap', ['symbolFactory', '$timeout', '$q', 'renderPointsOnMap', 'zoomMap', 'AutoSurveyControl', '$rootScope',
        function(symbolFactory, $timeout, $q, renderPointsOnMap, zoomMap, AutoSurveyControl, $rootScope) {
            var projectMap = L.Map.extend({
                curPointMarksLayer: null, //存放当前监测点marker的layer
                curPointTxtLayer: null, //存放当前监测点Label的layer
                remarkLayer: null, //存放标注信息的layer
                curPtNameHoverTime: 0, //记录用户在图表上悬停的时刻,当停留时间大于800ms时,就触发****事件
                autoSurveyMode: false,
                autoSurveyPrjName: '',
                autoSurveyControl: null,
                initialize: function(divId, options) { //重载构造函数
                    var map = this;
                    L.Map.prototype.initialize.call(this, divId, options);
                    if (options.autoSurveyMode) {
                        this.autoSurveyMode = options.autoSurveyMode;
                        if (this.autoSurveyMode) {
                            map.autoSurveyControl = new AutoSurveyControl();
                            map.autoSurveyControl.addTo(map);
                        }
                    }
                    if (options.autoSurveyPrjName) {
                        this.autoSurveyPrjName = options.autoSurveyPrjName;
                    }
                }
            });

            projectMap.prototype.clearPointsLayer = function() {
                if (this.curPointMarksLayer) {
                    this.curPointMarksLayer.clearLayers();
                }
                if (this.curPointTxtLayer) {
                    this.curPointTxtLayer.clearLayers();
                }
            };
            var loadEachLayer = function(map, layers, index) {
                index = index ? index : 0;
                $timeout((function(index, map) {
                    return function() {
                        var layer = layers[index];
                        L.geoJson(layer.layer, {
                            onEachFeature: (function(map) {
                                return function(feature, layer) {
                                    if (feature.properties.Remark) {
                                        try {
                                            var txt = feature.properties.Remark;
                                            if (txt.length === 0) return;
                                            var center = [];
                                            if (feature.geometry.type === "Point") {
                                                center = [feature.geometry.coordinates[1], feature.geometry.coordinates[0]];
                                            } else {
                                                center = L.bounds(feature.geometry.coordinates[0]).getCenter();
                                                center = [center.y, center.x];
                                            }
                                            var labelIcon = L.divIcon({
                                                'html': txt,
                                                'className': 'div-label',
                                                'iconSize': {
                                                    x: 20,
                                                    y: 8
                                                }
                                            });
                                            map.remarkLayer.addLayer(L.marker(center, {
                                                icon: labelIcon
                                            }));
                                        } catch (err) {
                                            return;
                                        }
                                    }
                                };
                            })(map),
                            style: symbolFactory.Factory,
                            pointToLayer: function(feature, latlng) {
                                return L.circleMarker(latlng, symbolFactory.PointFactory(feature));
                            }
                        }).addTo(map);
                        index++;
                        if (index < layers.length) {
                            loadEachLayer(map, layers, index);
                        } else {
                            map.fireEvent('finishLoadingLayers');
                        }
                    };
                })(index, map), 20);
            };
            projectMap.prototype.toggleAutoSurvey = function(toggle, autoSurveyPrjName) {
                var map = this;
                if (typeof(toggle) === 'undefined') {
                    map.autoSurveyMode = !map.autoSurveyMode;
                } else {
                    map.autoSurveyMode = toggle;
                    map.autoSurveyPrjName = autoSurveyPrjName || "";
                }
                if (map.autoSurveyControl) {
                    map.removeControl(map.autoSurveyControl);
                    map.autoSurveyControl = null;
                }
                if (map.autoSurveyMode) {
                    map.autoSurveyControl = new AutoSurveyControl();
                    map.autoSurveyControl.addTo(map);
                }
            };
            projectMap.prototype.loadProjectLayers = function(layers) {
                var defer = $q.defer();
                var map = this;
                map.fireEvent('startLoadingLayers');
                map.curPointMarksLayer = new L.featureGroup().addTo(map);
                map.curPointTxtLayer = new L.featureGroup().addTo(map);
                map.remarkLayer = new L.featureGroup().addTo(map);
                loadEachLayer(map, layers);
                //map.fireEvent('finishLoadingLayers');
                map.curPointMarksLayer.clearLayers();
                map.curPointTxtLayer.clearLayers();
                defer.resolve();
                return defer.promise;
            };
            return projectMap;
        }
    ]);
});
